//
//  MainView.m
//  Tire
//
//  Created by 王奎卿 on 2017/3/21.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "MainView.h"
#import "MainData.h"
#import <UIImageView+WebCache.h>
#import "MainCollectCell.h"
#import "FirstCell.h"
#import "ThreeCell.h"

@implementation MainView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addSubview:self.theScrollView];
        [self addSubview:self.tableView];
        self.tableView.tableHeaderView = self.theScrollView;
//        [self addSubview:self.thePage];
        [self addSubview:self.thePage];
        [self showMainView];
    }
    return self;
}

- (void)showMainView
{
    [[MainData shardData]getImageData];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jieshou:) name:@"headimg" object:nil];
    
    self.backgroundColor = [UIColor whiteColor];
    
}

- (void)jieshou:(NSNotification *)notifi
{
    dic = notifi.object;
    [self showHeadView];
}

- (void)showHeadView
{
    float width = self.frame.size.width;
    
    for (int i = 0; i < [[[dic objectForKey:@"json"][0] objectForKey:@"subClass"] count]; i++) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(width*i, 0, width, 200)];
        [img sd_setImageWithURL:[[[dic objectForKey:@"json"][0] objectForKey:@"subClass"][i] objectForKey:@"img"]];
        [self.theScrollView addSubview:img];
    }
    
    _theScrollView.contentSize = CGSizeMake(width*[[[dic objectForKey:@"json"][0] objectForKey:@"subClass"] count]-1, 200);
    
    _theScrollView.pagingEnabled = YES;
//
//    _theScrollView.showsHorizontalScrollIndicator = NO;
//
//    _theScrollView.delegate = self;
//    
//    _thePage.numberOfPages = [[[dic objectForKey:@"json"][0] objectForKey:@"subClass"] count];
//    
//    _thePage.userInteractionEnabled = NO;
//    
//    _thePage.currentPage = 0;
//    _thePage.backgroundColor = [UIColor clearColor];
//    
//    _thePage.pageIndicatorTintColor = [UIColor yellowColor];
//    _thePage.currentPageIndicatorTintColor = [UIColor redColor];
//    
//    _theInte = 0;
//    
//    _theTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
}

- (UIScrollView *)theScrollView
{
    if (!_theScrollView)
    {
        CGSize size = [UIScreen mainScreen].bounds.size;
        _theScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, size.width, 200)];
        
    }
    return _theScrollView;
}

- (UIPageControl *)thePage
{
    if (!_thePage)
    {
        CGSize size = [UIScreen mainScreen].bounds.size;
        _thePage = [[UIPageControl alloc]initWithFrame:CGRectMake(size.width/2-50, 200, 100, 30)];
    }
    return _thePage;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//-(void)scroll{
//    
//    _theInte ++;
//    
//    [_theScrollView setContentOffset:CGPointMake(_theInte * _theScrollView.frame.size.width, 0) animated:YES];
//    if (_theInte == [[dic objectForKey:@"json"] count]-1) {
//        [_theTimer setFireDate:[NSDate distantFuture]];
//    }
//}
//
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    int index = _theScrollView.contentOffset.x / _theScrollView.frame.size.width;
//    _thePage.currentPage = index;
//}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        CGSize size = [UIScreen mainScreen].bounds.size;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height) style:UITableViewStyleGrouped];
        
        _tableView.delegate = self;
        
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 9;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        
        FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111"];
        
        if (!cell)
        {
            cell = [[FirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"111"];
        }
        [cell clickBtBlock:^(NSInteger tag) {
            if(tag == 100){
                
                return @"11";
            }else if (tag == 101){
                
                return @"22";
            }else if (tag == 102){
                
                return @"33";
            }else if (tag == 103){
                
                return @"44";
            }
            return @"aa";
        }];
        return cell;
    }
    else if (indexPath.row == 1)
    {
        TwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"222"];
        cell.delegate = self;
        [cell.delegate jumpViewControl];
        
        if (!cell) {
            cell = [[TwoCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"222"];
        }
        [cell computeBlockGetUrl:^NSString *(NSString *str) {
            if([str isEqualToString:@"矿山车胎"]){
                
                return @"矿山";
            }else if ([str isEqualToString:@"自行车胎"]){
                
                return @"自行车";
            }else if ([str isEqualToString:@"摩托车胎"]){
                
                return @"摩托车";
            }else if ([str isEqualToString:@"工业车胎"]){
                
                return @"工业";
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return @"";
        }];
        return cell;
    }
    else
    {
        
        ThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aa"];
        
        if(!cell){
            
            cell = [[ThreeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"aa"];
        }
        
        
        cell.img.image = [UIImage imageNamed:@"111.jpg"];
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == 0){
        
        return 45;
    }else if (indexPath.row == 1){
        
        return 182;
    }
    return 140;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (indexPath.row == 1)
//    {
//        [self jumpViewControl];
//    }
}

- (void)jumpViewControl
{
    NSString *str = @"1502h";
    if (self.block != nil)
    {
        self.block(str);
    }
}

@end
